<?php
require_once(dirname(__FILE__).'/Abstract.php');
class Prescription extends AbstractPatientController 
{
    public function indexAction()
    {
    	$this->listingAction();
    }

    public function listingAction()
    {
    	$joinFeature = array();
        
        $joinFeature[] = array(
            'type'     => 'LEFT JOIN', // LEFT JOIN, RIGHT JOIN, INNER JOIN, etc...
            'table'    => '`medic_consultation` cons', // An existing table with alias 
            'onclause' => 'cons.id_consultation = ConsultationPrescriptionORM.id_consultation', // 
            'select'   => 'cons.id_doctor', // For select clause
        );
        $joinFeature[] = array(
            'type'     => 'LEFT JOIN', // LEFT JOIN, RIGHT JOIN, INNER JOIN, etc...
            'table'    => '`medic_user_entity` users', // An existing table with alias 
            'onclause' => 'users.id_user = cons.id_doctor', // 
            'select'   => 'users.lastname, users.firstname', // For select clause
        );
        $joinFeature[] = array(
            'type'     => 'LEFT JOIN', // LEFT JOIN, RIGHT JOIN, INNER JOIN, etc...
            'table'    => '`medic_user_doctor` doctors', // An existing table with alias 
            'onclause' => 'doctors.id_user = cons.id_doctor', // 
            'select'   => '', // For select clause
        );
        $joinFeature[] = array(
            'type'     => 'LEFT JOIN',
            'table'    => '`medic_user_role_lang` url',
            'onclause' => 'users.id_role = url.id_role AND url.lang = \''. $this->session->userdata('lang') .'\'',
            'select'   => 'url.`name` AS role_name',
        );
 
        $d = $this->initListing('ORM/Consultation/ConsultationPrescriptionORM', 'id_prescription', 'medic_consultation_prescription', false, null, null, $joinFeature);
        
        
        $d['title'] = $this->l('List of prescriptions');
        //$d['prescriptions'] = $this->getAllPrescriptions($this->session->userdata('id_user'));
        // Read all other prescription of this doctor
        //$d['prescriptions'] = $this->ConsultationMetier->getPrescriptions($config=array('id_doctor'=>(int)$this->session->userdata('id_user')));

        $d['title_for_layout'] = $this->l('prescriptions list.');
        $d['menu'] = 'patients';
        $d['page_name'] = 'prescriptions_list';
        
        $this->load->view('Templates/Layouts/Patient/header', $d);
        $this->load->view('Patient/Prescription/Listing', $d);
        $this->load->view('Templates/Layouts/Patient/footer', $d);

    }

    
    /* ==================================================
     *
     * PROTECTED FUNCTIONS
     * 
     * ================================================== */
    protected function getAllPrescriptions($id_doctor)
    {
                
        $prescription_table         = $this->ConsultationPrescriptionORM->getTableName();
        $prescription_table_primary = $this->ConsultationPrescriptionORM->getPrimaryKey();
        $cons_table                 = $this->ConsultationORM->getTableName();        
        $cons_table_primary         = $this->ConsultationORM->getPrimaryKey();
        $users_table                = $this->UserEntityORM->getTableName();        
        $users_table_primary        = $this->UserEntityORM->getPrimaryKey();
        $patients_table             = $this->UserPatientORM->getTableName();        
        $patients_table_primary     = $this->UserPatientORM->getPrimaryKey();
        
        //TODO : Enumarate fields to select because of date add 
        $this->db->select('precr.*, cons.*, users.*');
        $this->db->from($prescription_table.' as precr');
        $this->db->join($cons_table.' as cons', 'cons.'.$cons_table_primary.'='.'precr.'.$cons_table_primary, 'left');
        $this->db->join($users_table.' as users', 'cons.'.$patients_table_primary.'='.'users.'.$users_table_primary, 'left');
        $this->db->where('cons.id_doctor = '. (int)$id_doctor);
        
        $query = $this->db->get();

        return $query->result();
    }

    protected function getAllConsultations($id_doctor)
    {
        
        $prescription_type_table = $this->ConsultationTypeORM->getTableName();
        $prescription_type_table_primary = $this->ConsultationTypeORM->getPrimaryKey();
        
        //TODO : Join with the consultation type lang table 
        $cons_table             = $this->ConsultationORM->getTableName();        
        $cons_table_primary     = $this->ConsultationORM->getPrimaryKey();
        $users_table            = $this->UserEntityORM->getTableName();        
        $users_table_primary    = $this->UserEntityORM->getPrimaryKey();
        $patients_table         = $this->UserPatientORM->getTableName();        
        $patients_table_primary = $this->UserPatientORM->getPrimaryKey();
        
        $this->db->select('cons.*, users.*, cons_type.*');
        $this->db->from($cons_table.' as cons');
        $this->db->join($users_table.' as users', 'cons.'.$patients_table_primary.'='.'users.'.$users_table_primary, 'left');
        $this->db->join($prescription_type_table.' as cons_type', 'cons.'.$prescription_type_table_primary.'='.'cons_type.'.$prescription_type_table_primary, 'left');
        $this->db->where('cons.id_doctor = '. (int)$this->session->userdata('id_user'));
        $query = $this->db->get();

        return $query->result();
    }

    protected function getPrescriptionById($id_prescription)
    {
        
        $prescription_table         = $this->ConsultationPrescriptionORM->getTableName();
        $prescription_table_primary = $this->ConsultationPrescriptionORM->getPrimaryKey();
        $cons_table                 = $this->ConsultationORM->getTableName();        
        $cons_table_primary         = $this->ConsultationORM->getPrimaryKey();
        $users_table                = $this->UserEntityORM->getTableName();        
        $users_table_primary        = $this->UserEntityORM->getPrimaryKey();
        $patients_table             = $this->UserPatientORM->getTableName();        
        $patients_table_primary     = $this->UserPatientORM->getPrimaryKey();
        
        //TODO : Enumerate fields to select because of date add 
        $this->db->select('precr.*, cons.*, users.*');
        $this->db->from($prescription_table.' as precr');
        $this->db->join($cons_table.' as cons', 'cons.'.$cons_table_primary.'='.'precr.'.$cons_table_primary, 'left');
        $this->db->join($users_table.' as users', 'cons.'.$patients_table_primary.'='.'users.'.$users_table_primary, 'left');
        $this->db->where('precr.id_prescription = '.$id_prescription);
        $query = $this->db->get();

        return $query->row_array();
    }
}

